import axios from 'axios'
import { API_BASE_URL } from '@/config'
import { ElNotification } from 'element-plus'
import { message } from 'ant-design-vue'

// 创建实例
const service = axios.create({
  baseURL: API_BASE_URL,
  timeout: 10000
})

// 请求拦截器
service.interceptors.request.use(
  config => {
    // 可在此添加全局逻辑（如 token 注入）
    // const token = localStorage.getItem('token')
    // if (token) {
    //     config.headers.Authorization = `Bearer ${token}`
    // }
    // config.headers['Content-Type'] = 'application/json;charset=utf-8'

    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  response => {
    // 成功响应（HTTP 200）
    return response.data // 正常返回数据
  },
  error => {
    console.log(error)
    return Promise.resolve({
      code:400,
      message: error.response?.data?.message || '网络错误',
    })
  }
)

// 封装核心请求函数
const request =
  method =>
  (url, params = {}, config = {}) => {
    return service({
      method,
      url,
      ...(method === 'get' ? { params } : { data: params }),
      ...config
    })
  }

// 导出具体方法
export const getAction = request('get')
export const postAction = request('post')
export const putAction = request('put')
export const delAction = request('delete')
